-- $Id: tableExplorerBranch.lua 16 2014-02-24 05:55:58Z diesal2010 $
local AddOnName, Env = ... local ADDON = Env[1]
-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalTools = LibStub('DiesalTools-1.0')
local DiesalStyle = LibStub('DiesalStyle-1.0')
local DiesalGUI 	= LibStub('DiesalGUI-1.0')
local DiesalMenu 	= LibStub('DiesalMenu-1.0')
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local sub, format, match, lower							= string.sub, string.format, string.match, string.lower
local tsort														= table.sort
local tostring													= tostring
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- ~~| TableExplorerBranch |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local Type 		= 'ConAccordianSection'
local Version 	= 1
-- ~~| TableExplorerBranch StyleSheets |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local styleSheet = {
	['button-highlight'] = {
		type			= 'texture',
		layer			= 'HIGHLIGHT',
		color			= 'ffffff',
		alpha			= .1,
	},
	['button-background'] = {			
		type			= 'texture',
		layer			= 'BACKGROUND',							
		color			= '333333',		
		offset 		= {0,0,-1,0},		
	},	
	['button-gloss'] = {			
		type			= 'texture',
		layer			= 'ARTWORK',		
		gradient		= 'VERTICAL',				
		color			= 'FFFFFF',		
		alpha			= 0,
		alphaEnd		= .10,		
		offset		= {0,0,-1,0},	
	},			
	['button-outline'] = {			
		type			= 'outline',
		layer			= 'ARTWORK',					
		color			= 'FFFFFF',		
		alpha			= .05,				
		offset		= {0,0,-1,0},		
	},	
	['button-top'] = {			
		type			= 'texture',
		layer			= 'BACKGROUND',		
		color			= '000000',	
		offset		= {0,0,0,nil},	
		height		= 1,	
	},
	['content-top'] = {
		type			= 'texture',
		layer			= 'ARTWORK',		
		color			= '000000',	
		offset		= {0,0,0,nil},	
		height		= 1,
	},
	['content-topLight'] = {
		type			= 'texture',
		layer			= 'ARTWORK',		
		color			= 'ffffff',
		alpha			= .04,	
		offset		= {-1,-1,-1,nil},	
		height		= 1,
	},
	['content-bottom'] = {
		type			= 'texture',
		layer			= 'ARTWORK',		
		color			= '202020',		
		offset		= {-1,-1,nil,0},	
		height		= 1,
	},	
}
local wireFrameSheet = {
	['frame-white'] = {
		type			= 'outline',
		layer			= 'OVERLAY',
		color			= 'ffffff',
		alpha			= .15,
	},
	['button-purple'] = {
		type			= 'outline',
		layer			= 'OVERLAY',
		color			= 'aa00ff',
		alpha			= .5,
	},
	['content-yellow'] = {
		type			= 'outline',
		layer			= 'OVERLAY',
		color			= 'fffc00',
		alpha			= .5,
	},
}

-- ~~| TableExplorerBranch Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local BLUE			= DiesalTools:GetTxtColor('00aaff')
local DARKBLUE		= DiesalTools:GetTxtColor('004466')
local ORANGE		= DiesalTools:GetTxtColor('ffaa00')
local DARKORANGE	= DiesalTools:GetTxtColor('4c3300')
local GREY			= DiesalTools:GetTxtColor('7f7f7f')
local DARKGREY		= DiesalTools:GetTxtColor('414141')
local WHITE			= DiesalTools:GetTxtColor('e6e6e6')
local RED			= DiesalTools:GetTxtColor('ff0000')
local GREEN			= DiesalTools:GetTxtColor('00ff2b')
local YELLOW		= DiesalTools:GetTxtColor('ffff00')
local LIGHTYELLOW	= DiesalTools:GetTxtColor('ffea7f')

-- ~~| TableExplorerBranch Methods |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local methods = {
	['OnAcquire'] = function(self)
		self:ApplySettings()
		self:AddStyleSheet(styleSheet)
		self:Show()
	end,
	['OnRelease'] = function(self)

	end,
	['ApplySettings'] = function(self)
		if not self.settings.sectionTable then return false end

		self:ClearAllPoints()
		self[self.settings.expanded and "Expand" or "Collapse"](self)
		self.button:SetHeight(self.settings.buttonHeight)
		self.text:SetText(self.settings.section)

		if self.settings.position == 1 then
			self:SetPoint('TOPLEFT')
			self:SetPoint('RIGHT')
		else
			local anchor = self.settings.parentObject.children[self.settings.position-1].frame
			self:SetPoint('TOPLEFT',anchor,'BOTTOMLEFT',0,0)
			self:SetPoint('RIGHT')
		end
		
		self:UpdateHeight()		
	end,
	['Build'] = function(self,sectionTable,position)
		self.settings.position		= position
		self.settings.sectionTable	= sectionTable
		self.settings.section		= sectionTable.section

		for i = 1, #sectionTable do			
			local option = DiesalGUI:Create('OPT'..sectionTable[i].type:lower())
			self:AddChild(option)
			option:SetParentObject(self)
			option:SetEventListener('OnOptionUpdate', function(this,event,name,type,value) ADDON:Debug("OptUpdate",name,type,value) end)			
			option:SetSettings({				
				position		= i,
				optionTable	= sectionTable[i],
				padTop		= self.settings.contentPadTop,				
			},true)
		end
			
		self:ApplySettings()		
	end,
	['Collapse'] = function(self)		
		self.settings.expanded = false
		self.arrow:SetTexCoord(DiesalTools:GetIconCoords(7,5,16,256,128))
		self.content:Hide()
		self:UpdateHeight()
	end,
	['Expand'] = function(self)		
		self.settings.expanded = true
		self.arrow:SetTexCoord(DiesalTools:GetIconCoords(5,5,16,256,128))
		self.content:Show()
		self:UpdateHeight()
	end,
	['SetDB'] = function(self,db)
		self.settings.db = db		
		self[db[self.settings.section] and 'Collapse' or 'Expand'](self)
	end,
	['UpdateHeight'] = function(self)
		local contentHeight = 0
		local settings = self.settings
		local children = self.children

		if settings.expanded then
			for i=1 , #children do
				contentHeight = contentHeight + children[i].frame:GetHeight()
			end
			contentHeight = contentHeight + self.settings.contentPadTop + self.settings.contentPadBot
		end
		self.content:SetHeight(contentHeight)
		self:SetHeight(settings.buttonHeight + contentHeight)
		settings.parentObject:UpdateHeight()
	end,
}
-- ~~| TableExplorerBranch Constructor |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local function Constructor()
	local self 		= DiesalGUI:CreateObjectBase(Type)
	local frame		= CreateFrame('Frame',nil,UIParent)
	self.frame		= frame
	-- ~~ Default Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.defaults = {
		contentPadTop	= 3,
		contentPadBot	= 2,
		expanded			= true,
		buttonHeight 	= 16,
	}
	-- ~~ Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- OnAcquire, OnRelease, OnHeightSet, OnWidthSet
	-- ~~ Construct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

	local button = self:CreateRegion("Button", 'button', frame)
	button:SetPoint('TOPRIGHT')
	button:SetPoint('TOPLEFT')	
	button:SetScript("OnClick", function(this,button)
		DiesalGUI:OnMouse(this,button)
		self.settings.db[self.settings.section] = self.settings.expanded
		self[not self.settings.expanded and "Expand" or "Collapse"](self)		
		
	end)

	local arrow = self:CreateRegion("Texture", 'arrow', button)
	DiesalStyle:StyleTexture(arrow,{
		offset 	= {1,nil,-1,nil},
		height	= 16,
		width		= 16,
		texFile	= 'DiesalGUIcons',
		alpha		= .7,
	})

	local text = self:CreateRegion("FontString", 'text', button)
	text:SetPoint("TOPLEFT",13,-4)
	text:SetPoint("RIGHT",-4,0)
	text:SetHeight(0)
	text:SetJustifyH("TOP")
	text:SetJustifyH("LEFT")
	text:SetWordWrap(false)

	local content = self:CreateRegion("Frame", 'content', button)
	content:SetPoint('TOPLEFT',button,'BOTTOMLEFT',0,0)
	content:SetPoint('TOPRIGHT',button,'BOTTOMRIGHT',0,0)
	content:SetHeight(10)
	-- ~~ Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	for method, func in pairs(methods) do
		self[method] = func
	end
	-- ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	DiesalGUI:RegisterObject(self)
	return self
end
DiesalGUI:RegisterObjectConstructor(Type,Constructor,Version)
